﻿

namespace BlueLotus7.Contract
{
    using System;
    using System.Runtime.Serialization;

    [DataContract]
    [KnownType(typeof(DateTime))]
    [KnownType(typeof(Guid))]
    public abstract class BaseObject
    {
        #region Property

        [DataMember]
        public Guid Key
        {
            get;
            protected set;
        }

        [DataMember]
        public DateTime CreatedStamp
        {
            get;
            protected set;
        }

        [DataMember]
        public Guid? CreatedBy
        {
            get;
            protected set;
        }

        [DataMember]
        public DateTime LastUpdatedStamp
        {
            get;
            protected set;
        }

        [DataMember]
        public Guid? LastUpdatedBy
        {
            get;
            protected set;
        }

        [DataMember]
        public ObjectStatus ObjectStatus
        {
            get;
            protected set;
        }

        [DataMember]
        public string Description
        {
            get;
            set;
        }

        #endregion

        public BaseObject(Guid key, DateTime createdStamp, DateTime lastUpdatedStamp)
            : this(key, createdStamp, null, lastUpdatedStamp, null)
        {
        }

        public BaseObject(Guid key, DateTime createdStamp, Guid? createdBy, DateTime lastUpdatedStamp, Guid? lastUpdatedBy)
        {
            this.Key = key;
            this.CreatedStamp = createdStamp;
            this.LastUpdatedStamp = lastUpdatedStamp;
            this.CreatedBy = createdBy;
            this.LastUpdatedBy = lastUpdatedBy;
        }

        public BaseObject()
            : this(Guid.NewGuid(), DateTime.Now, null, DateTime.Now, null)
        {
        }

        public BaseObject(Guid createdBy)
            : this(Guid.NewGuid(), DateTime.Now, createdBy, DateTime.Now, createdBy)
        {
        }

        public void UpdateStamp()
        {
            this.LastUpdatedStamp = DateTime.Now;
        }

        public void UpdateStamp(Guid updatedBy)
        {
            this.LastUpdatedStamp = DateTime.Now;
            this.LastUpdatedBy = updatedBy;
        }

        public virtual bool EqualWithKey(BaseObject targetObject)
        {
            return targetObject != null && targetObject.Key == this.Key;
        }
    }
}
